package com.xiaoge.libraryuse.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoge.basedevlibrary.base.GuoBaseActivity;
import com.xiaoge.libraryuse.R;
import com.xiaoge.libraryuse.adapter.MuliListAdapter;
import com.xiaoge.libraryuse.bean.MoreTypeBean;
import com.xiaoge.libraryuse.user_define_refresh_header.UserDefineRefreshHeader;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class SmartRefreshLayoutActivity extends GuoBaseActivity {

    private RecyclerView recyclerview;
    private SmartRefreshLayout refreshLayout;
    private List mData = new ArrayList();
    private static boolean isFirstEnter = true;

    private int[] icons = {R.mipmap.i1, R.mipmap.i2, R.mipmap.i3, R.mipmap.i4, R.mipmap.i5, R.mipmap.i1, R.mipmap.i2, R.mipmap.i3, R.mipmap.i4, R.mipmap.i5, R.mipmap.i1, R.mipmap.i2, R.mipmap.i3, R.mipmap.i4, R.mipmap.i5};
    private MuliListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_refresh_layout);

        initView();
        initListener();
        initData();
        initAdapter();
    }

    private void initView() {
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);

        // 设置自定义的header
        refreshLayout.setRefreshHeader(new UserDefineRefreshHeader(this));
        refreshLayout.setHeaderHeight(50);

        // 设置分割线
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        // GridLayoutManager layoutManager2 = new GridLayoutManager(this, 2);

        recyclerview.setLayoutManager(layoutManager);
//        DividerDecoration itemDecoration = new DividerDecoration(Color.WHITE, DpUtils.dp2px(this, 8), DpUtils.dp2px(this, 0.5f), DpUtils.dp2px(this, 0.5f));
//        itemDecoration.setDrawLastItem(false);
//        recyclerview.addItemDecoration(itemDecoration);


        //触发自动刷新
        if (isFirstEnter) {
            isFirstEnter = false;
            refreshLayout.autoRefresh();
        } else {
            // mAdapter.refresh(initData());
        }
    }


    /**
     * 初始化数据
     **/
    private void initData() {

        // 随机数 用来标记item界面的类型
        Random random = new Random();

        for (int i = 0; i < icons.length; i++) {
            MoreTypeBean more = new MoreTypeBean();
            more.pic = icons[i];
            // 随机三种类型的视图给每个bean
            more.type = random.nextInt(3);
            mData.add(more);
        }
    }

    /**
     * 初始化监听
     */
    private void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                mData.clear();
                initData();
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                mAdapter.notifyDataSetChanged();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                initData();
                refreshLayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 创建adapter
     */
    private void initAdapter() {
        mAdapter = new MuliListAdapter(mData);
        recyclerview.setAdapter(mAdapter);
    }
}
